package com.nanychuan.blogback.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nanychuan.blogback.pojo.Article;
import com.nanychuan.blogback.pojo.Category;
import com.nanychuan.blogback.pojo.Tag;
import com.nanychuan.blogback.pojo.User;
import com.nanychuan.blogback.service.ArticleService;
import com.nanychuan.blogbase.exception.BlogException;
import com.nanychuan.blogbase.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 往
 * 博客后台文章管理
 */
@Controller
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    //查询所有文章

    @RequestMapping("/blog/selectArticle")
    @ResponseBody
    public PageInfo<Article> articleList(int page, int pageSize, String title, HttpSession session){
        //获取当前登录用户发布的文章
        User user = (User) session.getAttribute("user");
        //分页查询
        PageHelper.startPage(page,pageSize);
        //查询文章
        List<Article> articles = articleService.selectArticle(user.getUid(),title);
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        return pageInfo;
    }
    //修改文章状态
    @RequestMapping("/blog/article/changeIsOpen")
    @ResponseBody
    public Result changeIsOpen(Article article){
        Result result = new Result();
        try {
            articleService.isOpen(article);
            result.setFlag(true);
            if (article.getIsOpen().equals("1")){
                result.setMessage("文章修改为私密");
            }else {
                result.setMessage("文章修改为公开");
            }
        }catch (BlogException e){
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    //异步查询文章栏目
    @RequestMapping("/blog/article/queryCategory")
    @ResponseBody
    public List<Category> queryCategory(){
        List<Category> categories = articleService.queryCategory();
        return categories;
    }

    //选中栏目，加载栏目下对应的标签
    @RequestMapping("/blog/article/queryTags")
    @ResponseBody
    public List<Tag> queryTags(String cid){
        List<Tag> tags = articleService.queryTags(cid);
        return tags;
    }

    //异步发布文章
    @RequestMapping("/blog/article/addArticle")
    @ResponseBody
    public Result addArticle(Article article,HttpSession session){
        Result result = new Result();
        try{
            //获取当前登录的用户
            User user = (User) session.getAttribute("user");
            //设置发布文章的用户id
            article.setUid(user.getUid());
            //调用方法发布文章
            article = articleService.addArticle(article);
            //返回前台消息
            result.setFlag(true);
            result.setMessage("文章发布成功");
            result.setT(article);
        }catch (BlogException b){
            result.setFlag(false);
            result.setMessage(b.getMessage());
        }
        return result;
    }
    //根据id查询当前栏目信息
    @RequestMapping("/blog/article/queryById")
    @ResponseBody
    public Article queryById(String id){
        Article article = articleService.queryById(id);
        return article;
    }

    //异步修改文章
    @RequestMapping("/blog/article/updateArticle")
    @ResponseBody
    public Result updateArticle(Article article,HttpSession session){
        Result result = new Result();
        try{
            //获取当前登录的用户
            User user = (User) session.getAttribute("user");
            //设置发布文章的用户id
            article.setUid(user.getUid());
            //调用方法发布文章
            article = articleService.updateArticle(article);
            //返回前台消息
            result.setFlag(true);
            result.setMessage("文章修改成功");
            result.setT(article);
        }catch (BlogException b){
            result.setFlag(false);
            result.setMessage(b.getMessage());
        }
        return result;
    }

    //异步删除文章
    @RequestMapping("/blog/article/deleteArticle")
    @ResponseBody
    public Result deleteArticle(String id){
        Result result = new Result();
        try{
            //调用方法发布文章
            articleService.deleteArticle(id);
            //返回前台消息
            result.setFlag(true);
            result.setMessage("文章删除成功");
        }catch (BlogException b){
            result.setFlag(false);
            result.setMessage(b.getMessage());
        }
        return result;
    }

}
